///新版本
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_ahj_app/TestViewModel.dart';
import 'package:flutter_ahj_app/app/gzt/gztViewModel.dart';
import 'package:flutter_ahj_app/app/my/view/subViews/UserInfo.dart';
import 'package:flutter_ahj_app/app/NewMine/MyViewModel.dart';
import 'package:flutter_ahj_app/base/cunchu/Constant.dart';
import 'package:flutter_ahj_app/base/cunchu/UserInfoUtils.dart';
import 'package:flutter_ahj_app/base/rouleutils/RouleUtils.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JText.dart';
import 'package:flutter_ahj_app/base/utils/ShowFiles/GetFilesPath.dart';
import 'package:flutter_ahj_app/base/utils/ShowFiles/GetImageWidget.dart';
import 'package:flutter_ahj_app/base/utils/ShowFiles/zjTopBar.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
import 'package:flutter_ahj_app/base/utils/eventBus.dart';
import 'package:flutter_ahj_app/base/utils/font_utils.dart';
import 'package:flutter_ahj_app/base/utils/mTopBar.dart';
import 'package:flutter_ahj_app/base/utils/screen_util.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';

import '../../../base/basewidget/view/BaseWidget.dart';

class GztView extends BaseStatefulWidget {
  @override
  State<StatefulWidget> initState() {
    // TODO: implement initState
    return GztViewState();
  }
}

class GztViewState extends BaseState<GztViewModel, GztView> {
  List _gztData = ['创建任务', '隐患登记', '排查任务', '隐患治理'];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Future.delayed(Duration.zero, () {
      _caidan();
    });
  }

  _caidan() {
    viewModel.getHomeMoudle();
    viewModel.getHomeMoudleStream().listen((event) {
      if (event['code'] == '0') {
        setState(() {
          _gztData.clear();
          for (var i = 0; i < event['data'].length; i++) {
            _gztData.add(event['data'][i]['name']);
          }
        });
      }
    }, onError: (e) {
      print(e);
    });
  }

  @override
  Widget initWidget() {
    return new Scaffold(
      backgroundColor: SQColor.theme_Bg_color,
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(50),
        child: Container(
          decoration: new BoxDecoration(
            gradient: new LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter, colors: [
              Color(0xFFBCD3FF),
              Color(0xFFD3E3FA)
            ]),
          ),
          child: new AppBar(
            centerTitle: true,
            elevation: 0,
            backgroundColor: Colors.transparent,
            title: JText(
              '工作台',
              color: SQColor.black_333333,fontSize: 18,
            ),
          ),
        ),
      ),
      body: _bodyView(),
    );
  }

  _bodyView() {
    return Column(
      children: [
        _yhView(),
        Expanded(
          child: SingleChildScrollView(
            child: Container(
              margin: EdgeInsets.fromLTRB(Dimens.dimens12, Dimens.dimens12, Dimens.dimens12, 0),
              height: 100,
              decoration: BoxDecoration(
                color: SQColor.white,
                borderRadius: new BorderRadius.all(Radius.circular(4.0)),
               ),
              child: Column(
                children: [
                 Expanded(child: _gztView(),)
                ],
              ),
            ),
          ),
        ),
      ],
    );
  }

  _gztView() {
    return Container(
      margin: EdgeInsets.only(top: 15),
      child: GridView.builder(
          physics: const NeverScrollableScrollPhysics(),
          shrinkWrap: true,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 4, childAspectRatio: 1.2),
          itemCount: _gztData.length,
          itemBuilder: (context, index) {
            return InkWell(
              onTap: () {
                _itemClick(_gztData[index]);
              },
              child: Column(
                children: [
                  Image.asset(
                    "img/home_${_gztData[index]}.png",
                    width: 40,
                    height: 40,
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 5),
                    child: JText(
                      _gztData[index],
                      color: Color(0xff3A4A61),
                      fontSize: 13,
                    ),
                  ),
                ],
              ),
            );
          }),
    );
  }

  _itemClick(String title) {
    bool _isLogin = isEmpty(UserInfoUtils.getUserToken());
    if(_isLogin) {
      Navigator.pushNamed(context, "/LoginPageView");
      return ;
    }
    switch (title) {
      case '创建任务':
        Navigator.pushNamed(context, '/CreateRWView', arguments: {});
        break;
      case '隐患登记':
        Navigator.pushNamed(context, '/YhdjPageView', arguments: '');
        break;
      case '排查任务':
        Navigator.pushNamed(context, '/PcrwView');
        break;
      case '隐患治理':
        Navigator.pushNamed(context, '/YhzlView');
        break;
      default:
    }
  }

  _yhView() {
    return Column(
      children: [
        Container(
          margin: EdgeInsets.only(left: 15, top: 18),
          child: Row(
            children: [
              Container(
                width: 3,
                height: 15,
                decoration: BoxDecoration(
                    borderRadius: new BorderRadius.all(Radius.circular(6.0)),
                    color: SQColor.theme_color),
              ),
              Container(
                margin: EdgeInsets.only(left: 5, bottom: 2),
                child: JText('隐患排查治理', color: SQColor.black_333333, fontSize: 14, fontWeight: FontWeight.w500,),

              ),
            ],
          ),
        ),
      ],
    );
  }

}
